package com.atguigu.srb.sms.controller;

import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.exception.BusinessException;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.result.Result;
import com.atguigu.srb.common.util.RegexValidateUtils;
import com.atguigu.srb.sms.client.CoreUserInfoClient;
import com.atguigu.srb.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lsyandwh:
 * @create 2022-12-07 16:42
 */
@Api(tags = "短信接口")
@RestController
@RequestMapping("/api/sms")
//@CrossOrigin
public class ApiSmsController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private CoreUserInfoClient coreUserInfoClient;

    @GetMapping("/testSentinel")
    public Result testSentinel(){

//        coreUserInfoClient.checkMobile("123");
        System.out.println("testSentinel");
        return Result.ok();
    }

    @ApiOperation("获取验证码")
    @GetMapping("/sendCode/{phone}")
    public Result sendCode(@ApiParam("手机号") @PathVariable String phone){

        //判断当前手机号是否为空
        Assert.notNull(phone, ResponseEnum.MOBILE_NULL_ERROR);

        //判断当前手机号格式是否正确
        boolean b = RegexValidateUtils.checkCellphone(phone);
        Assert.isTrue(b,ResponseEnum.MOBILE_ERROR);

        //判断手机号是否存在
        Boolean isTrue = coreUserInfoClient.checkMobile(phone);
        Assert.isTrue(! isTrue,ResponseEnum.MOBILE_EXIST_ERROR);

        smsService.sendCode(phone);

        return Result.ok().message("验证码发送成功");
    }
}
